<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>jueji-tester</title>
	<link rel="stylesheet" type="text/css" href="http://cdn.bootcss.com/jasmine/2.4.1/jasmine.min.css">

	<script type="text/javascript" src="http://cdn.bootcss.com/jasmine/2.4.1/jasmine.min.js"></script>
	<script type="text/javascript" src="http://cdn.bootcss.com/jasmine/2.4.1/jasmine-html.min.js"></script>
	<script type="text/javascript" src="http://cdn.bootcss.com/jasmine/2.4.1/boot.min.js"></script>
	
	<script src="../node_modules/ramda/dist/ramda.min.js"></script>
	<script src="../jueji.js"></script>

	<script>
	describe("基础测试",function(){
		it("函数流功能",function(){
			var fn=J.stream(R.add(100),R.divide(100));
			expect(fn(1)).toEqual(100/(100+1))
		});
		it("中断函数流功能",function(){
			function div(a){
				return function(b){
					return b==0 ? J.left("can not div 0")
					: a/b;
				}
			}
			var fn=J.stream(R.add(100),div(100));
			expect(R.pipe(fn,J.getLeft)(-100)).toBe("can not div 0");
		});

		it("异步函数功能",function(){
			function milldamfn(){
				return J.milldam(function(fn){
					setTimeout(function(){
						fn("setTimeout");
					},1000)
				});
			}
			expect(1).toBe(1);
			var r = J.dostream(null,milldamfn,function(a){
				a == "setTimeout" ? null 
				: alert("异步函数功能 测试失败")
				return a;
			});
			J.overMilldam(function(a){
				a == "setTimeout" ? 0 
				: alert("异步函数功能 测试失败")
			},r)
		})
	});
	describe("其它测试",function(){
		it("模式匹配功能",function(){
			var fn=J.match([
				J.stream(R.always("100"))
				,J.stream(R.always(1))
			]);
			expect(fn()).toEqual("100");
			function falseNext(a){
				return a ? a
				: J.left();
			}
			function eq(a){
				return function(b){
					return a == b
				}
			}
			var fn1=J.match([
				J.stream(eq(10),falseNext,R.always("A"))
				,J.stream(eq(20),falseNext,R.always("B"))
				,R.always("C")
			]);

			expect(fn1(10)).toEqual("A");
			expect(fn1(20)).toEqual("B");
			expect(fn1(30)).toEqual("C");
		});
		it("上下文功能",function(){
			var fn = J.stream(R.always("A"),J.ctx("a"),R.always(100),J.ctx("b"))
			var context = {};
			var r=fn(null,context);
			expect(context.a).toEqual("A");
			expect(context.b).toEqual(100);
			expect(r).toEqual(100);
		});
		it("自定义流行为",function(){
			//只要数字是0 结果永远是0
			J.addStream(R.is(Number),function(n){
				return function(fn,context){
					return n == 0 ? 0
					: fn(n,context);
				}
			});
			function add(a){
				return function(b){
					return a+b;
				}
			}
			var fn =J.stream(add(10),add(4),add(1));
			expect(fn(1)).toEqual(16);
			expect(fn(-14)).toEqual(0);
			expect(fn(-10)).toEqual(0);
		});
	});
	</script>
</head>
<body>
	
</body>
</html>